package hongcaosp.app.android.comment;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import hongcaosp.app.android.R;
import xlj.lib.android.base.toastcompat.ToastManager;

public class CommentDialog extends Dialog {
    private EditText commentEditText;
    private InputMethodManager inputMethodManager;
    private CommentCallBack commentCallBack;

    public CommentDialog(@NonNull Context context, CommentCallBack commentCallBack) {
        this(context, R.style.InputDialog, commentCallBack);
    }

    public CommentDialog(Context context, int themeResId, CommentCallBack commentCallBack) {
        super(context, themeResId);
        this.commentCallBack = commentCallBack;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_comment);
        setCanceledOnTouchOutside(true);

        Window window = getWindow();
        WindowManager.LayoutParams layoutParams;
        if (window != null) {
            layoutParams = window.getAttributes();
            layoutParams.gravity = Gravity.BOTTOM;
            layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
            window.setAttributes(layoutParams);
        }
        commentEditText = findViewById(R.id.input_et);

        findViewById(R.id.send_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = commentEditText.getText().toString();
                if (TextUtils.isEmpty(text)) {
                    ToastManager.getInstance().showToast("请说点什么");
                    return;
                }
                dismiss();
                if (commentCallBack != null) {
                    commentCallBack.onComment(commentEditText.getText().toString());
                }
            }
        });

        setSoftKeyboard();
    }

    @Override
    public void dismiss() {
        View view = getCurrentFocus();
        if(view instanceof TextView){
            InputMethodManager mInputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            mInputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
        }

        super.dismiss();
    }

    private void setSoftKeyboard() {
        commentEditText.setFocusable(true);
        commentEditText.setFocusableInTouchMode(true);
        commentEditText.requestFocus();

        //为 commentEditText 设置监听器，在 DialogFragment 绘制完后立即呼出软键盘，呼出成功后即注销
        commentEditText.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                inputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                if (inputMethodManager != null) {
                    if (inputMethodManager.showSoftInput(commentEditText, 0)) {
                        commentEditText.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    }
                }
            }
        });
    }

    public static CommentDialog create(Context context,CommentCallBack commentCallBack) {
        return new CommentDialog(context, commentCallBack);
    }


    public interface CommentCallBack {
        void onComment(String content);
    }

}
